const { DataTypes } = require('sequelize')
const sequelize = require('../config/database')
const Article = require('./article')

const Category = sequelize.define(
  'category',
  {
    cate_id: {
      type: DataTypes.INTEGER.UNSIGNED,
      primaryKey: true, // 主键
      autoIncrement: true, // 自动增长
    },
    cate_name: {
      type: DataTypes.STRING(100),
      defaultValue: '', // 默认值
    },
  },
  {
    freezeTableName: true, // 设置为 true，表示不改变表名
    tableName: 'category', // 指定实际的表名为 'category'
    timestamps: false,
  }
)

// 定义表关联
Category.hasMany(Article, {
  sourceKey: 'cate_id',
  foreignKey: 'cate_id',
})
Article.belongsTo(Category, { sourceKey: 'art_id', foreignKey: 'cate_id' })

module.exports = Category
